﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils;
using DevExpress.XtraBars;

namespace Sunboxsoft.WinForm.Shell.Utils
{
    class RibbonPageHeaderUtil
    {
        public static void ApplyToRibbonToolbar(RibbonControl ribbon, List<ActionNode> rootMenus)
        {
            ImageCollection imageCollection = ribbon.Images as ImageCollection;
            ImageCollection largeImageCollection = ribbon.LargeImages as ImageCollection;

            BarSubItem rootItem = new BarSubItem();
            rootItem.ImageIndex = 0;
            rootItem.MenuDrawMode = MenuDrawMode.SmallImagesText;
            rootItem.ShowMenuCaption = true;
            rootItem.MenuCaption = Sunboxsoft.ASMS.Client.UI.ModuleContext.Current.UserName;
            rootItem.Caption = Sunboxsoft.ASMS.Client.UI.ModuleContext.Current.UserName;
            ribbon.Items.Add(rootItem);
            ribbon.PageHeaderItemLinks.Add(rootItem);


            for (int index = 0; index < rootMenus.Count; index++)
            {
                ActionNode node = rootMenus[index];

                if (node.Children != null && node.Children.Count > 0)
                {
                    if (node.UIObject != null)
                    {
                        rootItem.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        BarSubItem barItem = new BarSubItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;
                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        ribbon.Items.Add(barItem);
                        rootItem.ItemLinks.Add(barItem);

                        ApplyToApplicationMenu(ribbon, barItem, node.Children);
                    }
                }
                else
                {
                    if (node.UIObject != null)
                    {
                        rootItem.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        DevExpress.XtraBars.BarItem barItem = new DevExpress.XtraBars.BarButtonItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;
                        if (node.ClickCallback != null)
                        {
                            barItem.ItemClick += new ItemClickEventHandler(new Action<object, object>((o1, o2) =>
                            {
                                node.ClickCallback(o1, o2);
                            }));
                        }
                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        ribbon.Items.Add(barItem);
                        rootItem.ItemLinks.Add(barItem);
                    }
                }
            }
        }

        public static void ApplyToApplicationMenu(RibbonControl ribbon, BarSubItem parentItem, List<ActionNode> children)
        {
            ImageCollection imageCollection = ribbon.Images as ImageCollection;
            ImageCollection largeImageCollection = ribbon.LargeImages as ImageCollection;
            for (int index = 0; index < children.Count; index++)
            {
                ActionNode node = children[index];

                if (node.Children != null && node.Children.Count > 0)
                {
                    if (node.UIObject != null)
                    {
                        parentItem.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        BarSubItem barItem = new BarSubItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;

                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        ribbon.Items.Add(barItem);
                        parentItem.ItemLinks.Add(barItem);

                        ApplyToApplicationMenu(ribbon, barItem, node.Children);
                    }
                }
                else
                {
                    if (node.UIObject != null)
                    {
                        parentItem.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        DevExpress.XtraBars.BarItem barItem = new DevExpress.XtraBars.BarButtonItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;

                        if (node.ClickCallback != null)
                        {
                            barItem.ItemClick += new ItemClickEventHandler(new Action<object, object>((o1, o2) =>
                            {
                                node.ClickCallback(o1, o2);
                            }));
                        }

                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        ribbon.Items.Add(barItem);
                        parentItem.ItemLinks.Add(barItem);
                    }
                }
            }
        }
    
    }
}
